<?php


namespace app\db\model;


use think\Db;
use think\db\Query;
use think\Model;
use think\model\Pivot;

class Student extends Pivot
{
    // 主键
    protected $pk = 'studentId';

    // 数据表名
    protected $name = 'student';


    // 自动时间戳 支持[timestamp/datetime/int(默认)]
//    protected $autoWriteTimestamp = true;
    protected $autoWriteTimestamp = 'datetime';

    // 创建时间
//    protected $createTime = '创建字段名称';
    protected $createTime = false;

    // 更新时间
//    protected $updateTime = '更新字段名称';
    protected $updateTime = false;

    // 只读字段
    protected $readonly = ['studentId','departmentId','majorId','classId'];

    // 类型转换
    protected $type = [
        'both' => 'date:Y-m-d'
    ];

    // 自动完成 auto(新增 && 更新)、insert
//    protected $auto     = [];
//    protected $insert   = [];
//    protected $update   = ['idCard'];

    // JSON字段
    protected $json = [
        "address"
    ];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    // 初始化
    protected static function init()
    {
//        parent::init(); // TODO: Change the autogenerated stub

        self::event('before_update',function ($query){
            echo "更新操作之前触发 <br> \n\r";
        });

        self::afterUpdate(function ($query){
            echo "更新操作完成触发 <br> \n\r";
        });
    }

    public static function getStudentName($arg)
    {
        return self::where('studentId', "$arg")->find()->getAttr('studentName');
    }

    // 获取器
    public function getActiveAttr($value)
    {
        $active = [0 => "未激活", 1 => "已激活"];

        return $active[$value];
    }

    // 修改器
    public function setIdCardAttr($value)
    {
        if(is_null($value))
        {
            return null;
        }
        return strtoupper($value);
    }

    public function getNothingAttr($v, $data)
    {
        $active = [0 => "未激活", 1 => "已激活"];

        return $active[$data['active']];
    }

    // 搜索器
    public function searchStudentNameAttr($query, $value)
    {
        $query->where("studentName", 'like', "%{$value}%");
    }

    public function searchClassIdAttr($query, $value, $data)
    {
        $query->where("classId", 'like', "%{$value}%");
        if (isset($data["order"])) {
            $query->order($data["order"]);
        }
    }

    // 查询范围

    public function scopeGenderMale(Query $query){
        $query->where('gender','男');
    }

    public function scopeGenderFemale(Query $query){
        $query->where('gender','女');
    }

    public function scopeGenderNull(Query $query){
        $query->where('gender','');
    }

    public function scopeStudentNameLike(Query $query,$value)
    {
        $query->where('studentName','like',"%{$value}%");
    }

    // 全局范围查询
    public function base(Query $query){
        $query->where([['gender','in',['','男','女']]]);
    }

    // 关联查询
    public function department(){

        // hasOne 一对一关联
        // 模型名称,外键名,本表字段名
        return $this->hasOne('Department','departmentId','departmentId');

    }

    public function classes(){

        // hasOne 一对一关联
        // 模型名称,外键名,本表字段名
        return $this->hasOne('ClassModel','classId','classId');

    }

    public function departmentMany(){
        // hasMany 一对多关联
        return $this->hasMany('Department','departmentId','departmentId');
    }


}
